package com.example.ex00_test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import android.util.Log;

public class TCPClient {

	private Socket client;

	public TCPClient(String ip, int port) {
		try {
			this.client = new Socket(ip, port);
		} catch (IOException e) {
			Log.e("TCPClient", "IOException");
		}
	}

	public String send(String msg) {
		OutputStream out;
		String message = "";
		try {
			out = client.getOutputStream();
			// 注意第二个参数据为true将会自动flush，否则需要需要手动操作out.flush()
			PrintWriter output = new PrintWriter(out, true);
			output.println(msg);
			BufferedReader input = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			// read line(s)
			message = input.readLine();
			Log.d("Tcp Demo", "message From Server:" + message);
			// client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message;
	}

	public String sendBt(byte[] msg) {
		OutputStream out;
		String message = "";
		try {
			out = client.getOutputStream();
			// 注意第二个参数据为true将会自动flush，否则需要需要手动操作out.flush()
			PrintWriter output = new PrintWriter(out, true);
			output.println(msg);
			BufferedReader input = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			// read line(s)
			message = input.readLine();
			Log.d("Tcp Demo", "message From Server:" + message);
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message;
	}

	/**
	 * 加密
	 * 
	 * @param content
	 *            需要加密的内容
	 * @param password
	 *            加密密码
	 * @return
	 */
	public static byte[] encrypt(String content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			byte[] byteContent = content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(byteContent);// 加密
			return result;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密
	 * 
	 * @param content
	 *            待解密内容
	 * @param password
	 *            解密密钥
	 * @return
	 */
	public static byte[] decrypt(byte[] content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(content);
			return result; // 加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
}